package com.changgou.search.service.impl;

import com.alibaba.fastjson.JSON;
import com.changgou.search.dao.ESManagerMapper;
import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.pojo.Sku;
import com.changgou.search.pojo.SkuInfo;
import com.changgou.search.service.ESManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @Author Tang
 * @Date 2020/9/16 14:47
 * @Description
 */
@Component
public class ESManagerServiceImpl implements ESManagerService {

    @Autowired
    //es索引查询模板对象
    private ElasticsearchTemplate elasticsearchTemplate;

    @Autowired
    //goods微服务暴露的Feign远程服务调用接口
    private SkuFeign skuFeign;

    @Autowired
    private ESManagerMapper esManagerMapper;

    /**
     * 创建索引库结构,通过controller层访问调用
     */
    @Override
    public void createMappingAndIndex() {
        /**
         *创建索引库
         * 根据实体类参数,指定索引库的名称和类型
         */
        elasticsearchTemplate.createIndex(SkuInfo.class);
        //创建映射,索引库结构和SkuInfo实体类关系映射
        elasticsearchTemplate.putMapping(SkuInfo.class);
    }

    /**
     * 导入全部sku集合进入到索引库,通过controller层访问调用
     */
    @Override
    public void importAll() {
        //查询sku集合
        List<Sku> skuList = skuFeign.findSkuListBySpuId("all");

        if (skuList == null || skuList.size() <= 0) {
            throw new RuntimeException("当前没有数据被查询到,无法导入索引库");
        }
        //skulist转换为json
        String josnSkuList = JSON.toJSONString(skuList);
        //将json转换成skuinfo
        List<SkuInfo> skuInfoList = JSON.parseArray(josnSkuList, SkuInfo.class);

        for (SkuInfo skuInfo : skuInfoList) {
            //将每一个sku的规格信息转换成map
            Map specMap = JSON.parseObject(skuInfo.getSpec(), Map.class);
            skuInfo.setSpecMap(specMap);
        }

        //导入索引库
        esManagerMapper.saveAll(skuInfoList);
    }

    /**
     * 根据spuid查询skuList,添加到索引库
     * 根据商品上架状态修改,监听队列动态添加
     * @param spuId
     */
    @Override
    public void importDataBySpuId(String spuId) {
        List<Sku> skuList = skuFeign.findSkuListBySpuId(spuId);

        if (skuList == null || skuList.size() <= 0) {
            throw new RuntimeException("当前没有数据被查询到,无法导入索引库");
        }
        //将skuList集合转换为json,将json转换成List<SkuInfo>
        String jsonSkuList = JSON.toJSONString(skuList);
        List<SkuInfo> skuInfoList = JSON.parseArray(jsonSkuList, SkuInfo.class);

        for (SkuInfo skuInfo : skuInfoList) {
            Map specMap = JSON.parseObject(skuInfo.getSpec(), Map.class);
            skuInfo.setSpecMap(specMap);
        }

        //添加索引库
        esManagerMapper.saveAll(skuInfoList);
    }

    /**
     * 删除索引库数据
     * 根据商品下架状态修改,监听队列动态删除
     * @param spuId
     */
    @Override
    public void delDataBySpuId(String spuId) {
        List<Sku> skuList = skuFeign.findSkuListBySpuId(spuId);

        if (skuList == null || skuList.size()<=0){
            throw new RuntimeException("当前没有数据被查询到,无法导入索引库");
        }
        for (Sku sku : skuList) {
            esManagerMapper.deleteById(Long.parseLong(sku.getId()));
        }
    }
}
